package com.chenx.study;

import com.chenx.study.entity.User;
import com.chenx.io.Resource;
import com.chenx.sqlSession.SqlSession;
import com.chenx.sqlSession.SqlSessionFactory;
import com.chenx.sqlSession.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;

/**
 * @author chenx
 * @date 2021/3/7-18:25
 */
public class IpsersistenceTest {

    @Test
    public void test1() throws Exception {
        InputStream resourceInputSteam = Resource.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().builder(resourceInputSteam);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = new User();
        user.setId(2);
       user.setUsername("tom");
//        User user1 = sqlSession.selectOne("user.selectOne",user);
//        System.out.println(user1);
//
//        List<User> selectList = sqlSession.selectList("user.selectList");
//        for (User u:selectList){
//            System.out.println(u);
//        }
        IUserDao iUserDao = sqlSession.getMapper(IUserDao.class);
//        List<User> userList = iUserDao.findAll();
//        for (User u: userList){
//            System.out.println(u);
//        }
        User user2 = iUserDao.findByCondition(user);
        System.out.println(user2);

    }
    @Test
    public void test2() throws Exception {
        InputStream resourceInputSteam = Resource.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().builder(resourceInputSteam);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = new User();
        user.setId(4);
        user.setUsername("lisi");
        IUserDao iUserDao = sqlSession.getMapper(IUserDao.class);
       int i = iUserDao.insertOne(user);
        System.out.println("操作："+i+"行数据");

    }
    @Test
    public void test3() throws Exception {
        InputStream resourceInputSteam = Resource.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().builder(resourceInputSteam);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = new User();
        user.setId(4);
        user.setUsername("lisi");
        IUserDao iUserDao = sqlSession.getMapper(IUserDao.class);
        int i = iUserDao.deleteOne(user);
        System.out.println("操作："+i+"行数据");

    }
    @Test
    public void test4() throws Exception {
        InputStream resourceInputSteam = Resource.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().builder(resourceInputSteam);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = new User();
        user.setId(3);
        user.setUsername("张三");
        IUserDao iUserDao = sqlSession.getMapper(IUserDao.class);
        int i = iUserDao.updateOne(user);
        System.out.println("操作："+i+"行数据");

    }
}
